package impls.classes;

import impls.agents.dialogs.CarAgentDialog;
import impls.agents.dialogs.PathChooseDialog;
import impls.agents.dialogs.StationAgentDialog;
import jade.Boot;

import java.awt.Button;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;

public class Launcher extends JFrame implements ActionListener {

	private static final long serialVersionUID = -3884818419453215105L;

	private JFrame mainWindow;

	private JMenuItem jMenuItem_init1;

	private JMenuItem jMenuItem_init2;

	private JMenu jMenu_init;

	private JMenuBar jMenuBar1;

	private TextField noCars;

	private TextField noGasStations;

	private Checkbox autoGenerate;

	private String start;

	private String path;

	private boolean abort;

	/**
	 * @param args
	 */

	public static void main(String[] args) {
		new Launcher();
	}

	public Launcher() {
		mainWindow = new JFrame();
		mainWindow.setTitle("Cruising Agents Launcher");
		mainWindow.setSize(400, 400);
		// mainWindow.add(new TextField(5));
		path = "";

		try {
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

			jMenuBar1 = new JMenuBar();
			setJMenuBar(jMenuBar1);
			jMenuBar1.setPreferredSize(new java.awt.Dimension(632, 20));

			jMenu_init = new JMenu();
			jMenuBar1.add(jMenu_init);
			jMenu_init.setText("Options");
			jMenu_init.setPreferredSize(new java.awt.Dimension(84, 14));
			jMenu_init.setArmed(true);

			{
				jMenuItem_init1 = new JMenuItem();
				jMenu_init.add(jMenuItem_init1);
				jMenuItem_init1.setText("Choose application path");
				jMenuItem_init1.addActionListener(this);
			}
			{
				jMenuItem_init2 = new JMenuItem();
				jMenu_init.add(jMenuItem_init2);
				jMenuItem_init2.setText("Exit");
				jMenuItem_init2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jMenuItem_init2ActionPerformed(evt);
					}

					private void jMenuItem_init2ActionPerformed(ActionEvent evt) {
						// Terminates the Application
						mainWindow.dispose();
						System.exit(0);
					}
				});
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		mainWindow.setJMenuBar(jMenuBar1);
		mainWindow.setLayout(new FlowLayout());
		mainWindow.getContentPane().add(new JLabel("Number of Car Agents: "));

		noCars = new TextField(5);
		noCars.setText("5");
		noGasStations = new TextField(5);
		noGasStations.setText("4");

		mainWindow.getContentPane().add(noCars);

		mainWindow.getContentPane().add(
				new JLabel("Number of Fuel Station Agents: "));
		mainWindow.getContentPane().add(noGasStations);

		Button create = new Button("Create");
		create.addActionListener(this);

		mainWindow.getContentPane().add(create);

		autoGenerate = new Checkbox("Start Agents with random values");
		autoGenerate.setState(true);

		mainWindow.getContentPane().add(autoGenerate);

		mainWindow.pack();
		mainWindow.setVisible(true);

		start = "start java jade.Boot gui:impls.agents.GraphicsAgent ";
	}

	public void actionPerformed(ActionEvent e) {
		// System.out.println(e.getActionCommand());
		if (e.getActionCommand().equals("Create")) {
			if (autoGenerate.getState()) {
				buildStrAndLaunch();
				startAgents();
			} else {
				new CarAgentDialog(this, Integer.valueOf(noCars.getText()));
				if (!abort) {
					new StationAgentDialog(this, Integer.valueOf(noGasStations
							.getText()));
				}

				if (!abort) {
					startAgents();
				}

			}

		}
		if (e.getActionCommand().equals("Choose application path")) {
			new PathChooseDialog(this);
		}

	}

	public void buildStrAndLaunch() {
		for (int i = 1; i <= Integer.valueOf(noCars.getText()); i++) {
			start = start + "CarAgent" + i + ":impls.agents.CarAgent ";
		}
		for (int i = 1; i <= Integer.valueOf(noGasStations.getText()); i++) {
			start = start + "StationAgent" + i + ":impls.agents.StationAgent ";
		}
		// System.out.println(start);
	}

	public void createCarsAutomatic(int fuel) {
		for (int i = 1; i <= Integer.valueOf(noCars.getText()); i++) {
			start = start + "CarAgent" + i + ":impls.agents.CarAgent(" + fuel
					+ ") ";
		}
		// System.out.println(start);
	}

	public void createStationsAutomatic(int price) {
		for (int i = 1; i <= Integer.valueOf(noGasStations.getText()); i++) {
			start = start + "StationAgent" + i + ":impls.agents.StationAgent("
					+ price + ") ";
		}
		// System.out.println(start);
	}

	public void createCars(ArrayList<Integer> l) {
		for (int i = 0; i < l.size(); i++) {
			start = start + "CarAgent" + i + ":impls.agents.CarAgent("
					+ l.get(i) + ") ";
		}
		// System.out.println(start);
	}

	public void createStations(ArrayList<Integer> l) {
		for (int i = 0; i < l.size(); i++) {
			start = start + "StationAgent" + i + ":impls.agents.StationAgent("
					+ l.get(i) + ") ";
		}
		// System.out.println(start);
	}

	public void setAbort() {
		abort = true;
	}

	public void startAgents() {
		String[] startString = { start };
		Boot.main(startString);
	}

	public String getPath() {
		return path;
	}

	public void setPath(String p) {
		path = p;
	}

}
